/**
 * Created by zhou on 2017/3/28.
 * update yanglq
 */

/**
 * 首页消息通知功能
 */
var notice = '<div id="mgContainer" onclick="fnShowMg()" >'+
    '<div id="li_mg" class="icon-003 menuIcon">通知</div>'+
    '<div class="used_menu menu_close" id="msb_menu">'+
    '<div class="used_menu_arrow"></div>'+
    '<ul class="used_menu_ul" id="msb_menu_ul"></ul>'+
    '</div>'+
    '</div>';
$("#reHelpTip_div").before(notice);
var showBox = '<div id="mgShowBox"style="display: none; position: absolute; top: 50px; left: 200px; width: 600px; height: 400px; background: #fff; border: 1px solid wheat; z-index: 999; -webkit-box-shadow: 3px 7px 10px; -moz-box-shadow: 3px 7px 10px; box-shadow: 3px 7px 10px;">'
	+'<div id="msb_h" style="width: 100%; height: 29px; font-size: 14px; background: wheat; cursor: move;">'
	+'<a href="javascript:void(0)" style="display: block; height: 20px; line-height: 29px; color: #333; float: left; padding-left: 10px; font-weight: bold;">消息面板</a>'
	+'</div>'
	+'<div id="msb_c" style="height: 280px; width: 100%; overflow: auto;"></div>'
	+'<div id="msb_c_atm" style="height: 60px; width: 100%; overflow: auto;"></div>'
	+'<div id="msb_f" style="background: wheat;">'
	+'<input id="msb_b3" value="关闭" type="button" style="width: 50px; height: 25px; margin: 5px;" onclick="closeMsb()" />'
	+'<a id="from_name" style="margin: 5px; float: right;">发送人</a>'
	+'</div></div>'
$("body").append(showBox);

function initDwr(id,userid,basePath){
//	dwr.engine.setOverridePath("http://localhost:8082/ta3message-server/dwr");
	dwr.engine.setActiveReverseAjax(true);  
	dwr.engine.setNotifyServerOnPageUnload(true);
	dwr.engine.setErrorHandler(function(message, ex){
		dwr.engine._debug("Error: " + ex.name + ", " + ex.message, true);
	});
	var intervalsArray = [1,3,10];
	dwr.engine.setRetryIntervals(intervalsArray);
	dwr.engine.setMaxRetries(100);
	DwrService.onPageLoad(id,userid);
	
	now_id =  id;
	now_userid = userid;
	basepath = basePath;
	fngetMessage(id,userid);
}
//yanglq 首页消息通知功能
var mgRecord = [];  //保存通知
var mghead_r = 0;   // 是否重新获取通知
var mgRid; //记录当前打开的通知 id
var now_id;
var now_userid;
var basepath;
function fnShowMg(){
	if(!mghead_r){
		if(mgRecord.length){
			var html ="";
			for(var i=0;i<mgRecord.length;i++){
				if(mgRecord[i].state == 1){
					html+="<li onclick='showMgContent(this)'><div class='used_menu_img icon-003 menuIcon'></div>"+
							"<div class='used_menu_name' style='width: 120px;overflow:hidden;'>"+mgRecord[i].title+"</div></li>";
				}else{
					html+="<li class='unread' onclick='showMgContent(this)'><div class='used_menu_img'><img src='indexue/images/tree-edit-find.png'/></div>"+
							"<div class='used_menu_name' style='width: 120px;overflow:hidden;'>"+mgRecord[i].title+"</div></li>";
				}
			}
			$("#msb_menu_ul").html(html);
		}else{
			$("#msb_menu_ul").html("<li><div class='used_menu_name'>没有消息</div></li>");
		}
		mghead_r=1;
	}
	var $li_mg = $("#li_mg");
	$li_mg.html("<p>通知</p>");
	/*		var left = $li_mg.offset().left - 100;
	 $("#msb_menu").css({display:'block',left:left});*/
	$("#msb_menu").css({display:'block'});
}
function showMgContent(liobj){
    var index = $(liobj).index();
    mgRid = mgRecord[index].mgid;
    var html="<div style=\"padding-top:6px;text-align:center;height:8%;background:url('indexue/worktable/console/style/images/home_ico.png') 0 -247px;\">"+mgRecord[index].title+"</div>"+
        "<div style='height:88%;padding-top:5px;overflow:auto;'>"+mgRecord[index].content+"</div>";
    $("#msb_c").html(html);
    $("#msb_c_atm").html(mgRecord[index].attachment);
    $("#from_name").html("from: "+mgRecord[index].name +" , "+mgRecord[index].mgdateShow.substr(0,11));
    $("#mgShowBox").css("display","block");
    if($(liobj).hasClass("unread")){
    	$.ajax({
			"url" : basepath+"message/messageSend!setMg2sign.do",
			"data":{"dto['mgid']":mgRecord[index].mgid},
			"type":"POST",
			"dataType":"json"
		});
		mgRecord[index].state=1;
		mghead_r=0;
    }
}
function fngetMessage(id,userid){
	$.ajax({
		"url" : basepath+"message/messageSend!getMg.do",
		"data":"",
		"success":function(data){
			if(data){
				mgRecord = data.fieldData.datalist;
				mghead_r = 0;
				mgPoint();
			}
		},
		"type":"POST",
		"dataType":"json"
	});
}
function dealAllMessage(data){
	mgRecord = data;
	mghead_r = 0;
	mgPoint();
}
function mgPoint(){
	var newMg = 0;
	for(var i=0;i<mgRecord.length;i++){
		if(mgRecord[i].state != '1'){
			newMg++;
		}
	}
	if(newMg){
		$("#li_mg").html("<p style='color:red'>通知("+newMg+")</p>");
	}
}
function closeMsb(){
	$("#mgShowBox").css("display","none");
}
$('#msb_h').mousedown(
		function (event) {
			var isMove = true;
			var abs_x = event.pageX - parseInt($('#mgShowBox').css('left'));
			var abs_y = event.pageY - parseInt($('#mgShowBox').css('top'));
			$(document).mousemove(function (event) {
						if (isMove) {
							var obj = $('#mgShowBox');
							obj.css({'left':event.pageX - abs_x, 'top':event.pageY - abs_y});
						}
					}
			).mouseup(
					function () {
						isMove = false;
					}
			);
		}
);

function mgAtmDownload(name){
	var url = basepath+"message/messageSend!mgAtmDownload.do" +"?_download_fileName="+name+"&_download_file="+mgRid;
	location.href = url;
}
//这个函数是提供给后台推送的时候  调用的
function messageDwr(msg){
	console.log(msg);
	mgRecord.push(msg);
	mghead_r = 0;
	mgPoint();
}
//当鼠标点击非弹出菜单处，菜单消失
$(window).mousedown(
    function(e) {
        var ev = e ? e : $.event.fix(window.event || e);
        var target = ev.target || ev.srcElement;
        // $("#userInfoPlan").hide();
        if(target.id!="mgContainer" && target.parentElement.id!="mgContainer" && !$(target).hasClass("used_menu_name")){
            $("#msb_menu").fadeOut();
        }
    });